const mixin = {
  methods: {
    formatDate,
    formatDateAll,
    confirm,
    uuid,
    awaitWrap
  },
  filters: {
    formatDate,
    formatDateAll
  }
};

export default mixin;
function formatDate(date, type = "s") {
  const _date = new Date(date);
  const y = _date.getFullYear();
  const mm = _date.getMonth() + 1;
  const day = _date.getDate();
  const h = _date.getHours();
  const m = _date.getMinutes();
  const s = _date.getSeconds();
  // 个位补零
  const _b = [y, mm, day, h, m, s].map(n => (n.toString()[1] ? n : "0" + n));
  // 解构赋值取值
  const [a, b, c, d, e, f] = _b;
  if (type.endsWith("d")) return `${a}-${b}-${c}`;
  return `${a}-${b}-${c} ${d}:${e}:${f}`;
}
function formatDateAll(date, type = "s") {
  const _date = new Date(date);
  const y = _date.getFullYear();
  const mm = _date.getMonth() + 1;
  const day = _date.getDate();
  const h = _date.getHours();
  const m = _date.getMinutes();
  const s = _date.getSeconds();
  // 个位补零
  const _b = [y, mm, day, h, m, s].map(n => (n.toString()[1] ? n : "0" + n));
  // 解构赋值取值
  const [a, b, c, d, e, f] = _b;
  if (type.endsWith("d")) return `${a}-${b}-${c} ${d}:${e}:${f}`;
}

function confirm(msg = "执行删除操作,是否继续?", title = "提示") {
  return new Promise((resolve, reject) => {
    this.$confirm(msg, title, {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(() => {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        resolve("success");
      })
      .catch(() => {
        this.$message({
          type: "info",
          message: "操作取消!"
        });
        // resolve("false");
      });
  });
}
/*
 * @description: uuid
 * @Author: Coder
 * @Date: 2020-01-20 09:27:44
 * @LastEditors: Coder
 */
function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  return uuid;
}

function awaitWrap(promise) {
  return promise.then(data => [null, data]).catch(err => [err, null]);
}
